package com.example.list.recyleview;

import android.content.Intent;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.binioter.guideview.Guide;
import com.binioter.guideview.GuideBuilder;
import com.demo.viewpager.VPMainActivity;
import com.example.list.R;
import com.example.list.SimpleComponent;



import java.util.ArrayList;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import tyrantgit.explosionfield.ExplosionField;

public class ListActivity extends AppCompatActivity {

    private RecyclerView rl;
    final ArrayList<TV_MSG> arrayList = new ArrayList<TV_MSG>();
    ArrayList s_list = new ArrayList<String>();

    private EditText et_search;

    ExplosionField mExplosionField;
    private String TAG = "maina";
    MediaPlayer mediaPlayer;
    private RadioGroup viewById;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);
        rl = (RecyclerView) findViewById(R.id.rl);
        mExplosionField = ExplosionField.attach2Window(this);


        arrayList.add(new TV_MSG("1", Color.RED));
        arrayList.add(new TV_MSG("2", Color.parseColor("#F4B50D")));
        arrayList.add(new TV_MSG("3", Color.YELLOW));
        arrayList.add(new TV_MSG("4", Color.GREEN));
        arrayList.add(new TV_MSG("5", Color.parseColor("#FFFF00")));
        arrayList.add(new TV_MSG("6", Color.BLUE));
        arrayList.add(new TV_MSG("7", Color.parseColor("#9F009A")));
        arrayList.add(new TV_MSG("1", Color.RED));
        arrayList.add(new TV_MSG("2", Color.parseColor("#F4B50D")));
        arrayList.add(new TV_MSG("3", Color.YELLOW));
        arrayList.add(new TV_MSG("4", Color.GREEN));
        arrayList.add(new TV_MSG("5", Color.parseColor("#FFFF00")));
        arrayList.add(new TV_MSG("6", Color.BLUE));
        arrayList.add(new TV_MSG("7", Color.parseColor("#9F009A")));
        arrayList.add(new TV_MSG("7", Color.parseColor("#9F009A")));
//单一布局
        final MyAdapter adapter = new MyAdapter(this, arrayList);

        final GridLayoutManager layoutManager = new GridLayoutManager(this, 3);
        rl.setLayoutManager(layoutManager);

        rl.setAdapter(adapter);


        //尾布局
        final MyAdapterWithFooter footerAdapter = new MyAdapterWithFooter(this, arrayList);

        final GridLayoutManager layoutManager2 = new GridLayoutManager(this, 4);
        rl.setLayoutManager(layoutManager2);

        rl.setAdapter(footerAdapter);
        for (int i = 0; i < arrayList.size(); i++) {
            s_list.add(arrayList.get(i).getText());
        }

        adapter.setOnItemClickListener(new MyAdapter.ItemClickListener() {
            @Override
            public void onItemClick(int position, View view) {

                View tagetview = layoutManager.findViewByPosition(position);    //2为recyclerView中item位置，
                //mLayoutManager为recyclre的布局管理器
                TextView tv_msg = (TextView) tagetview.findViewById(R.id.tv_msg);

//                LinearLayout layout = (LinearLayout) view; //获取布局中任意控件对象
//                TextView tv_msg = (TextView) layout.findViewById(R.id.tv_msg);


                for (int i = 0; i < arrayList.size(); i++) {
                    if (s_list.contains(tv_msg.getText().toString())) {
                        Log.d(TAG, "onItemClick: " + tv_msg.getText().toString());
                        int taget = s_list.indexOf(tv_msg.getText().toString());

//                        if (mediaPlayer!=null&&mediaPlayer.isPlaying()) {
//                            mediaPlayer.reset();
//
//                        }else {
////                            mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.explode);
////                            mediaPlayer.start();
//                        }
//                        if (mediaPlayer==null) {
//                            mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.explode);
//                            mediaPlayer.start();
//                        }
                        mediaPlayer = MediaPlayer.create(ListActivity.this, R.raw.explode);
                        mediaPlayer.start();
                        if (mediaPlayer != null && mediaPlayer.isPlaying()) {
                            mediaPlayer.stop();
                            mediaPlayer = MediaPlayer.create(ListActivity.this, R.raw.explode);
                            mediaPlayer.start();
                        }

                        mExplosionField.explode(view);
                        s_list.remove(taget);
                        arrayList.remove(taget);
                        adapter.notifyDataSetChanged();
                    }

                }
            }
        });


        View view = LayoutInflater.from(this).inflate(R.layout.layout_rl_head, rl, false);
        et_search = (EditText) findViewById(R.id.ma_et);
        et_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });

        et_search.post(new Runnable() {
            @Override
            public void run() {
                showGuideView();
            }
        });
    }
    public void showGuideView() {
        GuideBuilder builder = new GuideBuilder();
        builder.setTargetView(et_search)
                .setAlpha(150)
                .setHighTargetCorner(10)
                .setHighTargetPadding(10);
        builder.setOnVisibilityChangedListener(new GuideBuilder.OnVisibilityChangedListener() {
            @Override
            public void onShown() {

            }

            @Override
            public void onDismiss() {
                showGuideView2();
                rl.post(new Runnable() {
                    @Override
                    public void run() {
                        showGuideView2();
                    }
                });
            }
        });

        builder.addComponent(new SimpleComponent());
        Guide guide = builder.createGuide();
        guide.show(ListActivity.this);
    }

    public void showGuideView2() {
        GuideBuilder builder = new GuideBuilder();
        builder.setTargetView(rl)
                .setAlpha(5) //透明度，值越低越透明，越高越不透明
                .setHighTargetCorner(100)
                .setHighTargetPadding(10);
        builder.setOnVisibilityChangedListener(new GuideBuilder.OnVisibilityChangedListener() {
            @Override
            public void onShown() {

            }
            @Override
            public void onDismiss() {
//                showGuideView2();
            }
        });

        builder.addComponent(new SimpleComponent());
        Guide guide = builder.createGuide();
        guide.show(ListActivity.this);
    }


}
